<script setup lang="ts">
  import { presignedApi, upload2OSS } from '@/api/hc/oss';
  import { ref } from 'vue';

  const uploadUrl = ref<string>('');
  function getUploadUrl() {
    presignedApi({
      ossPath: '/zhm/test',
      fileName: 'a.txt',
      expiry: 60 * 60 * 2,
    }).then((res) => {
      console.log(res);
      uploadUrl.value = res.url;
    });
  }

  const handleBeforeUpload = (file) => {
    upload2OSS(uploadUrl.value, file).then((res) => {
      console.log(res);
    });
    // 阻止自动上传
    return false;
  };
</script>

<template>
  <div>
    <a-button type="primary" @click="getUploadUrl">生成链接</a-button>
    <a-upload
      :before-upload="
        ($event) => {
          handleBeforeUpload($event);
        }
      "
    >
      <a-button>
        <upload-outlined />
        Click to Upload
      </a-button>
    </a-upload>
  </div>
</template>

<style scoped lang="less"></style>
